unit uFormHistoricoSituacoesOS;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids, EuDBGrid,
  Vcl.StdCtrls, Vcl.ExtCtrls, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
  FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, Data.DB,
  FireDAC.Comp.DataSet, FireDAC.Comp.Client, Datasnap.DBClient, uDM;

type
  TformHistoricoSituacoesOS = class(TForm)
    pnlTop: TPanel;
    lb: TLabel;
    lbOS_Id: TLabel;
    EuDBGrid1: TEuDBGrid;
    cds: TClientDataSet;
    dts: TDataSource;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure CriarCDS;
    procedure CarregarSituacoes(const pOS_Id: Integer);
  public
    { Public declarations }
  end;

procedure HistoricoSituacoesOS(const pOS_Id: Integer);
var
  formHistoricoSituacoesOS: TformHistoricoSituacoesOS;

implementation

{$R *.dfm}

{ TformHistoricoSituacoesOS }

procedure HistoricoSituacoesOS(const pOS_Id: Integer);
var
  F: TformHistoricoSituacoesOS;
begin
  F := TformHistoricoSituacoesOS.Create(nil);
  try
    F.lbOS_Id.Caption := IntToStr(pOS_Id);
    F.CarregarSituacoes(pOS_Id);
    F.ShowModal;
  finally
    FreeAndNil(F);
  end;
end;

procedure TformHistoricoSituacoesOS.CarregarSituacoes(const pOS_Id: Integer);
var
  Q: TFDQuery;
begin
  Q := DM.CriarFDQuery;
  try
    Q.Open('SELECT * FROM OSSituacao WHERE OS_Id = ' + IntToStr(pOS_Id) + ' ORDER BY Id DESC');
    cds.ReadOnly := False;
    while not Q.Eof do
    begin
      cds.Insert;
      cds.FieldByName('Data').AsDateTime := Q.FieldByName('DataRegistro').AsDateTime;
      cds.FieldByName('Hora').AsDateTime := Q.FieldByName('HoraRegistro').AsDateTime;
      cds.FieldByName('Situacao').AsString := Q.FieldByName('Situacao').AsString;
      cds.FieldByName('Motivo').AsString := Q.FieldByName('Motivo').AsString;
      cds.Post;
      Q.Next;
    end;
    cds.ReadOnly := True;
    Q.Close;
  finally
    FreeAndNil(Q);
  end;
end;

procedure TformHistoricoSituacoesOS.CriarCDS;
begin
  with cds.FieldDefs do
  begin
    Add('Data', ftDate);
    Add('Hora', ftTime);
    Add('Situacao', ftString, 15);
    Add('Motivo', ftString, 50);
  end;
  cds.CreateDataSet;
end;

procedure TformHistoricoSituacoesOS.FormCreate(Sender: TObject);
begin
  CriarCDS;
end;

end.
